package com.cosina.game;

import com.cosina.game.colorball.BallGameView;
import com.cosina.game.colorball.BallView;
import com.cosina.game.colorball.Difficulty;
import com.cosina.game.colorball.Trophy;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;

public class BallGameStartActivity extends Activity {
	
	public static final String PREFERENCE_FILE_NAME = "records";
	public static final String PREFERENCE_RECORD = "-record";
	public static final String PREFERENCE_BREAKER = "-breaker";
	
	public Difficulty difficulty = Difficulty.EASY;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Trophy.init(this);
        BallView.init(this);
        adjustDifficulty();
    }

    
    public void adjustDifficulty(){
    	 AlertDialog.Builder selectDifficulty = new AlertDialog.Builder(this);
         selectDifficulty.setTitle("please select game");
         selectDifficulty.setItems(Difficulty.stringValues(), new DialogInterface.OnClickListener(){
 			@Override
 			public void onClick(DialogInterface dialog, int which) {
 				difficulty = Difficulty.getByCode(3 + which);
 				resetContent();
 			}});
         selectDifficulty.create().show();
    }
    
	@Override
	public void setRequestedOrientation(int requestedOrientation) {
		if(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE == requestedOrientation)
			super.setRequestedOrientation(requestedOrientation);
	}

	public void resetContent(){
		BallGameView ballGameView = new BallGameView(this,difficulty);
        ballGameView.setLayoutParams(new LayoutParams(-1,-1));
        setContentView(ballGameView);
	}
	
	private int NEW_GAME_BUTTON = Menu.FIRST;
	private int DIFFICULTY_BUTTON = Menu.FIRST + 1;
	private int RECORDS_BUTTON = Menu.FIRST + 2;
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, NEW_GAME_BUTTON, Menu.NONE, "New Game");
		menu.add(Menu.NONE, DIFFICULTY_BUTTON, Menu.NONE, "Difficulty");
		menu.add(Menu.NONE, RECORDS_BUTTON, Menu.NONE, "records");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == NEW_GAME_BUTTON){
			resetContent();
			return true;
		}else if(item.getItemId() == DIFFICULTY_BUTTON){
			adjustDifficulty();
			return true;
		}else if(item.getItemId() == RECORDS_BUTTON){
			Trophy.showRecord(BallGameStartActivity.this);
			return true;
		}
		return false;
	}
	
	
    
    
}